package com.zlk.algorithm.dataStructure.linkList03;

/**
 * @program: algorithm
 * @ClassName Code02_DeleteGivenValue
 * @description:在链表中删除指定值的所有节点
 * @author: slfang
 * @create: 2024-12-12 11:01
 * @Version 1.0
 **/
public class Code02_DeleteGivenValue {

    public static SimpleNode removeValue(SimpleNode head, int num) {
        // head来到第一个不需要删的位置
        while (head != null) {
            if (head.value != num) {
                break;
            }
            head = head.next;
        }
        // 1 ) head == null
        // 2 ) head != null
        SimpleNode pre = head;
        SimpleNode cur = head;
        while (cur != null) {
            if (cur.value == num) {
                pre.next = cur.next;
            } else {
                pre = cur;
            }
            cur = cur.next;
        }
        return head;
    }
}
